/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package planner_exercise;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author Propietario
 */
public class TextProcessor {

	BufferedReader br;
	Items game;
	ArrayList<String> text;
                
	public TextProcessor(String filename, Items game) {
		try {
			br = new BufferedReader(new InputStreamReader(TextProcessor.class.getResourceAsStream(filename)));
		} catch (Exception e) {
		}
		text = new ArrayList<>();
                this.game=game;
	}
	public Items read() {
            try {                
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();

                    while (line != null) {
                            sb.append(line);
                            sb.append('\n');
                            text.add(line);
                            line = br.readLine();                           
                    }
                    //String everything = sb.toString();
                    br.close();
                    game = parseText(text);
                    return game;
                    //return everything;

            } catch (IOException e) {             
            }
            return null;
	}

    /**
     *
     * @param text
     * @return
     */
    public Items parseText(ArrayList<String> text) {
		//wagons
		String wagon[] = text.get(0).substring(7).split(",");
		for(String w : wagon) game.wagons.add(w);
		
                //initialState
                String initialStateText[] = text.get(1).substring(14).split(";");
            for (String iSText : initialStateText) {
                String ist[];
                ist = iSText.split("\\(|\\,|\\)");
                // we parse for Predicates that are type(obj1,obj2)
                Predicates p;
                if(ist.length==1){
                    p=new Predicates(ist[0]);
                    game.initialState.add(p);
                }
                if(ist.length==3){
                    p=new Predicates(ist[0],ist[1],ist[2]);
                    game.initialState.add(p);

                }
                if(ist.length==2){
                    int count=0;
                    try{
                        // parse for Predicates that are type(int railNum)
                        
                        int a=Integer.parseInt(ist[1]);
                        p=new Predicates(ist[0],a);
                        game.initialState.add(p);

                        count++;
                    }catch(NumberFormatException e){}
                    // parse for Predicates that are type(obj1)

                    if(count==0){
                        p=new Predicates(ist[0],ist[1]);
                        game.initialState.add(p);

                    }
                }
            }

		//finalState
		String finalStateText[] = text.get(2).substring(11).split(";");
            for (String fSText : finalStateText) {
                String fst[];
                fst = fSText.split("\\(|\\,|\\)");
                // we parse for Predicates that are type(obj1,obj2)
                Predicates p;
                // we parse for Predicates that are type;
                if(fst.length==1){
                    p=new Predicates(fst[0]);
                    game.finalState.add(p);
                }
                if(fst.length==3){
                    p=new Predicates(fst[0],fst[1],fst[2]);
                    game.finalState.add(p);

                }
                if(fst.length==2){
                    int count=0;
                    try{
                        // parse for Predicates that are type(int railNum)
                        int a=Integer.parseInt(fst[1]);
                        p=new Predicates(fst[0],a);
                        game.finalState.add(p);
                        
                        count++;
                    }catch(NumberFormatException f){}
                    // parse for Predicates that are type(obj1)

                    if(count==0){
                        p=new Predicates(fst[0],fst[1]);
                        game.finalState.add(p);

                    }
                }
            }	
		return game;
	}
}
